package m5;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class Producer {

    public static void main(String[] args) throws IOException, TimeoutException {

        //1. 连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c = f.newConnection().createChannel();

        //2. 创建 topic 交换机,路由
        c.exchangeDeclare("topic_logs", BuiltinExchangeType.TOPIC);

        //3. 发送消息
        while (true) {

            System.out.println("输入消息：");
            String s = new Scanner(System.in).nextLine();
            System.out.println("输入路由键：");
            String k = new Scanner(System.in).nextLine();

            // 第一个参数为交换机
            // 第二个参数为路由键，默认交换机的路由键就是队列名
            c.basicPublish("topic_logs", k, null, s.getBytes());

        }

    }

}
